package com.example.swaggerDemo.controller;


import com.example.swaggerDemo.entity.ArticleEntity;
import com.example.swaggerDemo.entity.MsgEntity;
import com.example.swaggerDemo.entity.articleUserEntity;
import com.example.swaggerDemo.service.ArticleService;
import com.example.swaggerDemo.service.articleUserService;
import com.example.swaggerDemo.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@Api("文章列表页")
@RestController
@RequestMapping(value="/Article")
public class ArticleController {
    @Autowired
    private ArticleService Service;

    @Autowired
    private articleUserService articleUserService;

    @ApiOperation(value="查询",notes="查询所有文章信息")
    @ResponseBody
    @RequestMapping(value="/findAll",method= RequestMethod.POST)
    public Object findAll(@RequestParam("pageindex") int pageindex, @RequestParam("pagesize") int pagesize) {
        MsgEntity msg=new MsgEntity();
        try {
            List<ArticleEntity>  dto = Service.findAll(pageindex,pagesize);
            if ( dto.size()>0){
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @ApiOperation(value="查询",notes="通过id获取文章详情")
    @ResponseBody
    @RequestMapping(value="/findid",method= RequestMethod.POST)
    public Object findid(@RequestParam("id") String id,@RequestParam("upopenid") String upopenid,@RequestParam("userid") String userid) {
        MsgEntity msg=new MsgEntity();
        try {
            ArticleEntity dto = Service.findid(id);
            if ( dto!=null){
                if (userid!=""&&userid!=null) {
                    articleUserEntity userEntity = articleUserService.findid(userid, id);
                    if (userEntity == null) {
                        Service.updateReadcountl(dto.getReadcount() + 1, id);
                        articleUserService.insert(userid, id);

                    }
                }
                if (upopenid!=""&&upopenid!=null)
                {
                    SessionUtil.setSessionAttribute("upopenid", upopenid); // 设置 Session 中的 userid
                }
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @ApiOperation(value="添加",notes="添加广告")
    @ResponseBody
    @RequestMapping(value="/insert",method= RequestMethod.POST)
    public Object insert(  @RequestParam("title") String title,
                           @RequestParam("pretext") String pretext,
                           @RequestParam("preimage") String preimage,
                           @RequestParam("content") String content) {
        MsgEntity msg=new MsgEntity();
        try {
          int dto = Service.inserarticle(title,pretext,preimage,content);
            if ( dto>0){
                msg.msg="数据添加成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="添加失败";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @ApiOperation(value="修改",notes="修改广告")
    @ResponseBody
    @RequestMapping(value="/update",method= RequestMethod.POST)
    public Object update(
            @RequestParam("id") String id,
            @RequestParam("title") String title,
                               @RequestParam("pretext") String pretext,
                               @RequestParam("preimage") String preimage,
                               @RequestParam("content") String content) {
        MsgEntity msg=new MsgEntity();
        int dto = Service.update(id,title,pretext,preimage,content);
        if ( dto>0){
            msg.msg="数据添加成功";
            msg.code=true;
            msg.object=dto;
        }
        else
        {
            msg.msg="添加失败";
            msg.code=false;
            msg.object=dto;
        }
        return msg;
    }

    @ApiOperation(value="删除",notes="删除广告")
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value="/delete",method= RequestMethod.POST)
    public Object delete(@RequestParam("id") String id) {
        MsgEntity msg=new MsgEntity();
        int dto = Service.delete(id);
        if ( dto>0){
            msg.msg="数据删除成功";
            msg.code=true;
            msg.object=dto;
        }
        else
        {
            msg.msg="删除失败";
            msg.code=false;
            msg.object=dto;
        }
        return msg;
    }
}
